package com.thinkfit.jy.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.blankj.utilcode.util.GsonUtils;
import com.common.lib.bean.UserBean;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.BrandInfoActivity;
import com.thinkfit.jy.activity.VersionActivity;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.LogUtils;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ToastUtils;
import com.thinkfit.jy.utils.Utils;
import com.thinkfit.jy.view.MyCircleProgress;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 自由运动信息卡片界面
 */
public class FreeMovementItem2Fragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    int type = 0;
    float num = 0;
    TextView tvTime,tvDistance,tvKcal,tvStepNum,tvSpm,tvPace,tvRealTimeHr,tvAverageHr,tvMaximumHr,tvTarget,tvTargetTip
            ,tvSpmUnit,tvSpmTip,tvStepNumTip;
    ImageView ivTarget;
    ConstraintLayout clHeart,clTarget;

    MyCircleProgress prTarget;

    public static FreeMovementItem2Fragment getInstance(int type,float num) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.BUNDLE_EXTRA, type);
        bundle.putFloat(Constants.BUNDLE_EXTRA_2, num);
        FreeMovementItem2Fragment fragment = new FreeMovementItem2Fragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_free_movement_item_2;
    }


    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        type = getArguments().getInt(Constants.BUNDLE_EXTRA);
        num = getArguments().getFloat(Constants.BUNDLE_EXTRA_2);
        LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx  type : "+type+"  num : "+num);
        tvTime = view.findViewById(R.id.tv_time);
        tvDistance = view.findViewById(R.id.tv_distance);
        tvKcal = view.findViewById(R.id.tv_kcal);
        tvStepNum = view.findViewById(R.id.tv_step_num);
        tvSpm = view.findViewById(R.id.tv_spm);
        tvPace = view.findViewById(R.id.tv_pace);
        clHeart = view.findViewById(R.id.cl_heart);
        clTarget = view.findViewById(R.id.cl_target);
        tvRealTimeHr = view.findViewById(R.id.tv_real_time_hr);
        tvAverageHr = view.findViewById(R.id.tv_average_hr);
        tvMaximumHr = view.findViewById(R.id.tv_maximum_hr);
        ivTarget = view.findViewById(R.id.iv_target);
        tvTargetTip = view.findViewById(R.id.tv_target_tip);
        tvTarget = view.findViewById(R.id.tv_target);
        prTarget = view.findViewById(R.id.pr_target);
        tvSpmUnit = view.findViewById(R.id.tv_spm_unit);
        tvSpmTip = view.findViewById(R.id.tv_spm_tip);
        tvStepNumTip = view.findViewById(R.id.tv_step_num_tip);
        if(type != 0){
            clTarget.setVisibility(View.VISIBLE);
        }else {
            clTarget.setVisibility(View.GONE);
        }
        if(DeviceTypeUtils.getDeviceType() != 2){
            tvSpmUnit.setText(getString(R.string.app_rpm));
            tvSpmTip.setText(getString(R.string.app_speed_rate));
            tvStepNumTip.setText(getString(R.string.app_speed_count));
        }

        initImageView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }

        if (map.containsKey(EventBusEvent.START_RUN)) {
//            ArrayList<Integer> target = SportsStatusBarViewUtils.getInstance().getTarget();
//            type = Integer.valueOf(target.get(0));
//            num = Integer.valueOf(target.get(1));
//            if(type == 0){
//                tvSportsCenter.setText(getString(R.string.app_free_training));
//            }else {
//                tvSportsCenter.setText(SportsStatusBarViewUtils.getInstance().targetType(type, num));
//            }
        }else if (map.containsKey(EventBusEvent.STOP_RUN)){
//            clSportInfo.setVisibility(View.GONE);
//            tvSportsCenter.setText(getString(R.string.app_sports_center));
            num = 0;
            type = 0;
//            stopInitView();
        }else if(map.containsKey(EventBusEvent.PAUSE_RUN)){

        }else if (map.containsKey(EventBusEvent.DEVICE_DATA)) {
            System.out.println("MotionDataUtils -------------> ddddddddddddddddddddddd  CurrentInfo : "+ GsonUtils.toJson((SerialPortManager.CurrentInfo) map.get(EventBusEvent.DEVICE_DATA)));
            SerialPortManager.CurrentInfo currentInfo = (SerialPortManager.CurrentInfo) map.get(EventBusEvent.DEVICE_DATA);
            resetInfo(currentInfo);
        }else if(map.containsKey(EventBusEvent.EQUIPMENT_STATE)){//设备状态
//            SerialPortManager.CurrentInfo currentInfo = (SerialPortManager.CurrentInfo) map.get(EventBusEvent.EQUIPMENT_STATE);
            SerialPortManager.CurrentInfo currentInfo = MotionDataUtils.getInstance().getCurrentInfo();
            resetInfo(currentInfo);
        }else if(map.containsKey(EventBusEvent.SET_TARGET)){
            ArrayList<String> o = (ArrayList<String>) map.get(EventBusEvent.SET_TARGET);
            type = Integer.valueOf(o.get(0));
            num = Integer.valueOf(o.get(1));

        }
    }

    int maxHeart = 0;
    int hearts = 0;
    int heartItem = 0;

    int targetItem = 0;
    int time;
    public void resetInfo(SerialPortManager.CurrentInfo info) {
        if(time == info.time){
            return;
        }
        time = info.time;
        float kcal = info.kcal /10f;
        int hour = info.time / 3600;
        int minute = (info.time / 60) % 60;
        int second = info.time % 60;

        float v = (60f / (info.speed/10f))*60;
        int paceHour = (int) (v/3600);
        int paceMin = (int) ((v/ 60) % 60);
        int paceSecond = (int) v % 60;
        if(info.heart > maxHeart){
            maxHeart = info.heart;
        }
        if(info.heart != 0){
            hearts += info.heart;
            heartItem ++;
        }
        if (paceHour > 0) {
            tvPace.setText(BaseUtils.StaticParams.getNewText(paceHour) + ":"
                    + BaseUtils.StaticParams.getNewText(paceMin) + "'"
                    + BaseUtils.StaticParams.getNewText(paceSecond)+"''");
        } else {
            tvPace.setText(BaseUtils.StaticParams.getNewText(paceMin) + "'"
                    + BaseUtils.StaticParams.getNewText(paceSecond)+"''");
        }

        if (hour > 0) {
            tvTime.setText(BaseUtils.StaticParams.getNewText(hour) + ":"
                    + BaseUtils.StaticParams.getNewText(minute) + ":"
                    + BaseUtils.StaticParams.getNewText(second));
        } else {
            tvTime.setText(BaseUtils.StaticParams.getNewText(minute) + ":"
                    + BaseUtils.StaticParams.getNewText(second));
        }
        tvKcal.setText(Utils.getNewFloat(kcal));
        tvDistance.setText(Utils.getNewFloat2(info.distance / 1000f));
        tvSpm.setText(info.rpm+"");
        tvRealTimeHr.setText(info.heart+"");
        tvStepNum.setText(info.count+"");
        if(hearts != 0 && heartItem != 0){
            tvAverageHr.setText((hearts/heartItem)+"");
        }
        if(maxHeart != 0){
            tvMaximumHr.setText(maxHeart+"");
        }
        if(type == 1){
            if((targetItem-info.distance) >= 0){
                prTarget.SetCurrent(info.distance);
                tvTarget.setText(((targetItem-info.distance)/1000f)+"");
            }else {
                ToastUtils.getInstance().show(getContext(),getString(R.string.app_target_complete));
            }
        }else if(type == 2){
            if((targetItem - info.time) >= 0){
                prTarget.SetCurrent(info.time);
                tvTarget.setText(targetTime(targetItem - info.time));
            }else {
                ToastUtils.getInstance().show(getContext(),getString(R.string.app_target_complete));
            }

        }else if(type == 3){
            if((targetItem - info.kcal) >= 0){
                prTarget.SetCurrent(info.kcal);
                tvTarget.setText(((targetItem - info.kcal)/10)+"");
            }else {
                ToastUtils.getInstance().show(getContext(),getString(R.string.app_target_complete));
            }
        }

    }

    public String targetTime(int time){
        String str = null;
        int hour = time / 3600;
        int minute = (time / 60) % 60;
        int second = time % 60;
        if (hour > 0) {
            str = BaseUtils.StaticParams.getNewText(hour) + ":"
                    + BaseUtils.StaticParams.getNewText(minute) + ":"
                    + BaseUtils.StaticParams.getNewText(second);
        } else {
            str = BaseUtils.StaticParams.getNewText(minute) + ":"
                    + BaseUtils.StaticParams.getNewText(second);
        }
        return str;
    }

    @Override
    public void onStop() {
        super.onStop();
        hearts = 0;
        heartItem = 0;
        maxHeart = 0;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    public void initImageView(){
        if(type == 1){
            ivTarget.setImageResource(R.drawable.img_free_distance);
            tvTargetTip.setText(getString(R.string.app_target_xxx_km,num+""));
            targetItem = Math.round(num*1000f);
        }else if(type == 2){
            ivTarget.setImageResource(R.drawable.img_free_time);
            tvTargetTip.setText(getString(R.string.app_target_xxx_minutes,num+""));
            targetItem = Math.round(num*60f);
        }else if(type == 3){
            ivTarget.setImageResource(R.drawable.img_free_kcal);
            tvTargetTip.setText(getString(R.string.app_target_xxx_kcal,num+""));
            targetItem = Math.round(num*10f);
        }
        prTarget.SetMax(targetItem);
    }
}
